'use strict';
/**
 * Created by FreezeSoul on 2015/9/30.
 */
define([
        'angular',
        'app/core/loader',
        'app/modules/index/define',
        'app/modules/index/services/index'
    ],
    function (angular, plugin) {
        angular.module('ad.index')
            .controller('adIndexAddController', ['$scope', '_', '$uibModalInstance', 'adIndexEditService', 'item', function ($scope, _, $uibModalInstance, service, item) {
                $scope.item = {
                    chart_id: -1,
                    chart_name: '',
                    chart_data: '',
                    chart_type_id: '',
                    chart_folder_id: item.data.folder_id
                };

                $scope.selectType = "";

                $scope.types = [];
                $scope.charts = [];

                $scope.toggle = function (item) {
                    $scope.item.chart_type_id = item.uid;
                };

                $scope.toggleType =  function(type){
                    $scope.selectType = type;
                };

                plugin.initManifests().then(function () {
                    var manifests = arguments;
                    _.each(manifests, function (manifest) {
                        if (manifest.type.indexOf('hide') == -1) {
                            if ($scope.types.indexOf(manifest.type) == -1) {
                                $scope.types.push(manifest.type);
                            }
                            $scope.charts.push({
                                uid: manifest.uid,
                                name: manifest.name,
                                type: manifest.type,
                                desc: manifest.description,
                                image: "app\\plugins\\" + manifest.path + "\\" + manifest.icon
                            });

                            $scope.selectType = $scope.types[0];
                        }
                    })
                });

                $scope.ok = function () {
                    service.addChart($scope.item).then(function (data) {
                        $uibModalInstance.close(data);
                    });
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }]);
    });